package com.molyfun.parents.activity.organization.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.organization.SignUpDetailActivity;
import com.molyfun.parents.rest.bean.OrganizationListResponse;
import com.molyfun.parents.rest.bean.SignUpListResponse;
import com.molyfun.parents.view.TwoTabBar;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:9/19/16.
 */
public class SigUpEnteringListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * TYPE_ADDED 已加入
     * TYPE_CHECK 审核中
     */
    private int mCurrentTab = TwoTabBar.TwoTabChooseTab.BAR_ONE;
    private Context mContext;
    private int mOldChoosePosition = -1;
    private int mSelectedPosition = -1;

    private List<SignUpListResponse.DataBean.RecordsBean> mSignUpEnteringList;

    public SigUpEnteringListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void refreshData(List<SignUpListResponse.DataBean.RecordsBean> list, int tab) {
        this.mSignUpEnteringList = list;
        mCurrentTab = tab;
        notifyDataSetChanged();
    }

    public void clearAll() {
        if (mSignUpEnteringList != null) {
            mSignUpEnteringList.clear();
            notifyDataSetChanged();
        }
    }

    public SignUpListResponse.DataBean.RecordsBean getChoosedOrganization() {
        if (mOldChoosePosition == mSelectedPosition) {
            return null;
        }
        return mSignUpEnteringList.get(mSelectedPosition);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SignUpListViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_organization_list_cell, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        SignUpListViewHolder organizationViewHolder = (SignUpListViewHolder) holder;
        final SignUpListResponse.DataBean.RecordsBean organizationBean = mSignUpEnteringList.get(position);

        organizationViewHolder.mOrganizationCheckState.setVisibility(View.GONE);
        organizationViewHolder.mOrganizationChecking.setVisibility(View.VISIBLE);
        organizationViewHolder.mOrganizationChecking.setText(organizationBean.getStatus());
        organizationViewHolder.mOrganizationName.setText(organizationBean.getDepartname());
        organizationViewHolder.mlist_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 切换选择的机构
                mSelectedPosition = position;
                notifyDataSetChanged();
                SignUpDetailActivity.startActivity(mContext, organizationBean.getDepartid(), organizationBean.getId());
            }
        });
        if (organizationBean.getStatus().equals("通知入学") && organizationBean.getStatus().equals("已缴费")) {
            //TODO 通知入学刷新数据
            if(sigUpEnteringListCallBack != null) {
                sigUpEnteringListCallBack.refreshSigUpEnteringListData();
            }
        }

    }

    @Override
    public int getItemCount() {
        if (mSignUpEnteringList == null) {
            return 0;
        } else {
            return mSignUpEnteringList.size();
        }
    }

    class SignUpListViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.list_layout)
        RelativeLayout mlist_layout;
        @InjectView(R.id.organization_check_state)
        ImageView mOrganizationCheckState;
        @InjectView(R.id.organization_name)
        TextView mOrganizationName;
        @InjectView(R.id.organization_check)
        TextView mOrganizationChecking;
        @InjectView(R.id.student_remarkname)
        TextView mStudentRemarkName;

        public SignUpListViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }

    }

    public interface SigUpEnteringListCallBack {
        void refreshSigUpEnteringListData();
    }
    private SigUpEnteringListCallBack sigUpEnteringListCallBack ;
    public void setSigUpEnteringListListener(SigUpEnteringListCallBack sigUpEnteringListCallBack) {
        this.sigUpEnteringListCallBack = sigUpEnteringListCallBack;
    }
}
